#!/usr/bin/python3

import os
import time

ds18b20 = ''


def setup():
    global ds18b20
    for i in os.listdir('/sys/bus/w1/devices'):
        if i != 'w1_bus_master1':
            ds18b20 = i


def read_temp():
    location = '/sys/bus/w1/devices/' + ds18b20 + '/w1_slave'
    t_file = open(location)
    text = t_file.read()
    t_file.close()
    second_line = text.split("\n")[1]
    temperature_data = second_line.split(" ")[9]
    temperature = float(temperature_data[2:])
    temperature = temperature / 1000
    return temperature


def loop():
    while True:
        if read_temp() is not None:
            print("Current temperature : %0.3f C" % read_temp())
            time.sleep(1)


def destroy():
    pass


if __name__ == '__main__':
    try:
        setup()
        loop()
    except KeyboardInterrupt:
        destroy()
